﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace e_yayincilik_webproject
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {


        //////////////////////////////////////////////////////////////////////////////////////////////////
        //Makale

        public List<DBclass.MakaleDBTO> OnaylananMakaleListele(){
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            
            var sec = from k in entities.MakalePathDurums where k.MakaleDurumu=="onaylandi"
                      select k ;
            /*var res = from r in entities.Makalelers where r.MakaleID==sec.ToString()
                      select r; */
            var res = from si in entities.Makalelers
                                 join s in sec on si.MakaleID equals s.MakaleID 
                                 select si;         

            List<DBclass.MakaleDBTO> abc = res.ToList().ConvertAll(new Converter<Makaleler, DBclass.MakaleDBTO>(Converter.Convert_Makale_TO_MakaleDBTO));
            return abc;
        }

        public DBclass.MakaleDBTO MakaleBul(string makaleId)
        {
           EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
           Makaleler res = (from r in entities.Makalelers where r.MakaleID == makaleId select r).First();
           return Converter.Convert_Makale_TO_MakaleDBTO(res);
        }

        public void MakaleEkle(DBclass.MakaleDBTO obj, DBclass.MakalePathDurumuDBTO obj2)
        {
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            Makaleler insertingobject = Converter.Convert_MakaleDBTO_TO_Makele(obj);
            /*insertingobject.BilimTuruID =obj.BilimTuruID;
            insertingobject.DergidekiSira =obj.DergidekiSira;
            insertingobject.DergiID = obj.DergiID;
            insertingobject.KategoriID = obj.KategoriID;
            insertingobject.Kaynaklar = obj.Kaynaklar;
            insertingobject.MakaleAdi = obj.MakaleAdi;
            insertingobject.MakaleID = obj.MakaleId;
            insertingobject.YayinTarihi = obj.YayinTarihi;
            insertingobject.YazarID = obj.YazarId;
            insertingobject.YazimTarihi = obj.YazimTarihi;
            */

            entities.AddToMakalelers(insertingobject);
            MakalePathDurum insertMakale = Converter.Convert_MakalePathDurumuDBTO_TO_MakalePathDurum(obj2);
           /* insertMakale.Makale_path = obj2.Makale_path;
            insertMakale.MakaleDurumu = obj2.MakaleDurumu;
            insertMakale.MakaleID = obj2.MakaleID;
            */
            entities.AddToMakalePathDurums(insertMakale);

            entities.SaveChanges();
        }

        public void MakaleGuncelle(DBclass.MakaleDBTO obj, DBclass.MakalePathDurumuDBTO obj2)
        {
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            Makaleler res = (from r in entities.Makalelers where r.MakaleID == obj.MakaleId select r).First();
            res.BilimTuruID = obj.BilimTuruID;
            res.DergidekiSira = obj.DergidekiSira;
            res.DergiID = obj.DergiID;
            res.KategoriID = obj.KategoriID;
            res.Kaynaklar = obj.Kaynaklar;
            res.MakaleAdi = obj.MakaleAdi;
            res.YayinTarihi = obj.YayinTarihi;
            res.YazimTarihi = obj.YazimTarihi;

            MakalePathDurum sec = (from r in entities.MakalePathDurums where r.MakaleID == obj2.MakaleID select r).First();
            sec.Makale_path = obj2.Makale_path;
            sec.MakaleDurumu = obj2.MakaleDurumu;
            entities.SaveChanges();

        }

        public void MakaleSil(string MakaleId)
        {
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            MakalePathDurum res = (from r in entities.MakalePathDurums where r.MakaleID == MakaleId select r).First();
            res.MakaleDurumu = "silindi";
            entities.SaveChanges();
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////
        //Dergi

        public List<DBclass.DergiDBTO> DergiListele()
        {

            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            var res = from r in entities.Dergis select r;
            List<DBclass.DergiDBTO> abc = res.ToList().ConvertAll(new Converter<Dergi, DBclass.DergiDBTO>(Converter.Convert_Dergi_TO_DergiDBTO));
            return abc;

        }


        public DBclass.DergiDBTO DergiBul(string dergiId)
        {
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            Dergi res = (from r in entities.Dergis where r.DergiID == dergiId select r).First();
            return Converter.Convert_Dergi_TO_DergiDBTO(res);
        }

        public void DergiEkle(DBclass.DergiDBTO obj) {
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            Dergi insertingobject = Converter.Convert_DergiDBTO_TO_Dergi(obj);
           /* insertingobject.BasimTarihi = obj.BasimTarihi;
            insertingobject.BilimDaliID = obj.BilimDaliID;
            insertingobject.DergiAdi = obj.DergiAdi;
            insertingobject.DergiID = obj.DergiId;
            insertingobject.EditorID = obj.EditorId;
            insertingobject.KonuKategorisiID = obj.KonuKategorisiID;
            insertingobject.MakaleSayisi = obj.MakaleSayisi;
            insertingobject.YayinTarihi = obj.YayinTarihi;
            */
            entities.AddToDergis(insertingobject);
            entities.SaveChanges();
        }


        public void DergiGuncelle(DBclass.DergiDBTO obj) {

            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            Dergi res = (from r in entities.Dergis where r.DergiID == obj.DergiId select r).First();

            res.BasimSayisi = obj.BasimSayisi;
            res.BasimTarihi = obj.BasimTarihi;
            res.BilimDaliID = obj.BilimDaliID;
            res.DergiAdi = obj.DergiAdi;
            res.EditorID = obj.EditorId;
            res.KonuKategorisiID = obj.KonuKategorisiID;
            res.MakaleSayisi = obj.MakaleSayisi;
            res.YayinTarihi = obj.YayinTarihi;

            entities.SaveChanges();
        }

        public void DergiSil(string dergiId)
        {
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            Dergi res = (from r in entities.Dergis where r.DergiID == dergiId select r).First();
         
            entities.DeleteObject(res);
            entities.SaveChanges();
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////
        //Kategori 


        public List<DBclass.KategoriDBTO> KategoriListele()
        {
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            var res = from r in entities.Kategorilers select r;
            List<DBclass.KategoriDBTO> abc = res.ToList().ConvertAll(new Converter<Kategoriler, DBclass.KategoriDBTO>(Converter.Convert_Kategori_TO_KategoriDBTO));
            return abc;
        }

        public DBclass.KategoriDBTO KategoriBul(string kategoriId)
        {
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            Kategoriler res = (from r in entities.Kategorilers where r.KategoriID == kategoriId select r).First();
            return Converter.Convert_Kategori_TO_KategoriDBTO(res);
        }

        public void KategoriEkle(DBclass.KategoriDBTO obj){
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            Kategoriler insertingobject = Converter.Convert_KategoriDBTO_TO_Kategori(obj);
           /* insertingobject.KategoriID = obj.KategoriId;
            insertingobject.BilimTuruID = obj.BilimTuruId;
            insertingobject.Kategori = obj.Kategori;*/
            entities.AddToKategorilers(insertingobject);
            entities.SaveChanges();
        }

        public void KategoriGuncelle(DBclass.KategoriDBTO obj){
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            Kategoriler res = (from r in entities.Kategorilers where r.KategoriID == obj.KategoriId select r).First();

            res.Kategori = obj.Kategori;

            entities.SaveChanges();
        }

        public void KategoriSil(string KategoriId,string bilimturuID){
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            //first olmaz kategoriye göre silinmeli 
            //Kategoriler res = (from r in entities.Kategorilers where r.KategoriID == KategoriId and r.BilimTuruID=bilimturuID select r).First();
             Kategoriler del = (from si in entities.Kategorilers
                                 join s in entities.Kategorilers on si.BilimTuruID equals bilimturuID
                                 where s.KategoriID==KategoriId
                                 select si).First();      
            entities.DeleteObject(del);
            entities.SaveChanges();
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////
        //BilimDali

  
        public List<DBclass.BilimTuruDBTO> BilimTuruListele(){
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            var res = from r in entities.BilimTurus select r;
            List<DBclass.BilimTuruDBTO> abc = res.ToList().ConvertAll(new Converter<BilimTuru, DBclass.BilimTuruDBTO>(Converter.Convert_BilimTuru_TO_BilimTuruDBTO));
            return abc;
        }

        public DBclass.BilimTuruDBTO BilimTuruBul(string bilimTuruId){

            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            BilimTuru res = (from r in entities.BilimTurus where r.BilimTuruID == bilimTuruId select r).First();
            return Converter.Convert_BilimTuru_TO_BilimTuruDBTO(res);
        }

        public void BilimTuruEkle(DBclass.BilimTuruDBTO obj){
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            BilimTuru insertingobject = Converter.Convert_BilimTuruDBTO_TO_BilimTuru(obj);
          /*  insertingobject.BilimTuruID = obj.BilimTuruID;
            insertingobject.BilimTuru1 = obj.BilimTuru;
           */
            entities.AddToBilimTurus(insertingobject);
            entities.SaveChanges();
        }

        public void BilimTuruGuncelle(DBclass.BilimTuruDBTO obj)
        {
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            BilimTuru res = (from r in entities.BilimTurus where r.BilimTuruID == obj.BilimTuruID select r).First();

            res.BilimTuru1 = obj.BilimTuru;
            entities.SaveChanges();
        }

        public void BilimTuruSil(string bilimDaliId)
        {
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            BilimTuru res = (from r in entities.BilimTurus where r.BilimTuruID == bilimDaliId select r).First();
            Kategoriler resed = (from r in entities.Kategorilers where r.BilimTuruID==bilimDaliId select r).First();
            entities.DeleteObject(resed);
            entities.DeleteObject(res);
            entities.SaveChanges();
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////
        //Kullanici

        public void KullaniciEkle(DBclass.KullaniciDBTO obj)
        {
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
             /*  insertingobject.AdiSoyadi = obj.AdiSoyadi;
            insertingobject.Eposta = obj.Eposta;
            insertingobject.KullaniciAdi = obj.KullaniciAdi;
            insertingobject.Sifre = obj.Sifre;
            insertingobject.SistemdekiRolu = obj.SistemdekiRolu;*/
            Kullanici insertingobject = Converter.Convert_KullaniciDBTO_TO_Kullanici(obj);
        
            entities.AddToKullanicis(insertingobject);
            entities.SaveChanges();
        }

        public void KullaniciGuncelle(DBclass.KullaniciDBTO obj)
        {
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            Kullanici res = (from r in entities.Kullanicis where r.KullaniciAdi == obj.KullaniciAdi select r).First();
            res.Sifre = obj.Sifre;
            res.Eposta = obj.Eposta;
            res.AdiSoyadi = obj.AdiSoyadi;
            entities.SaveChanges();
        }

        public void KullaniciAtama(DBclass.KullaniciDBTO obj, string sistemdekiRolu)
        {
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities()  ;
            Kullanici res = (from r in entities.Kullanicis where r.KullaniciAdi == obj.KullaniciAdi select r).First();
            res.SistemdekiRolu = sistemdekiRolu;
            if (sistemdekiRolu == "hakem")
            {
                DBclass.HakemDBTO hakem =new DBclass.HakemDBTO();
                hakem=hakem.addHakemDBTO("h"+obj.KullaniciAdi,obj.KullaniciAdi);
                Hakem insertingobject = Converter.Convert_HakemDBTO_TO_Hakem(hakem);
                entities.AddToHakems(insertingobject);
            }
            else if (sistemdekiRolu == "yazar")
            {
                DBclass.YazarDBTO yazar = new DBclass.YazarDBTO();
                yazar = yazar.addYazarDBTO("y" + obj.KullaniciAdi, obj.KullaniciAdi);
                Yazar insertingobject = Converter.Convert_YazarDBTO_TO_Yazar(yazar);
                entities.AddToYazars(insertingobject);
            }
            else if (sistemdekiRolu == "editor")
            {
                DBclass.EditorDBTO editor = new DBclass.EditorDBTO();
                editor = editor.addEditorDBTO("e" + obj.KullaniciAdi, obj.KullaniciAdi);
                Editor insertingobject = Converter.Convert_EditorDBTO_TO_Editor(editor);
                entities.AddToEditors(insertingobject);
            }
            else if (sistemdekiRolu == "admin")
            {
                DBclass.AdminDBTO admin = new DBclass.AdminDBTO();
                admin = admin.addAdminDBTO("a" + obj.KullaniciAdi, obj.KullaniciAdi);
                Admin insertingobject = Converter.Convert_AdminDBTO_TO_Admin(admin);
                entities.AddToAdmins(insertingobject);
            }
            entities.SaveChanges();
        }

        public void KullaniciSil(string KullaniciId, string sistemdekiRolu)
        {
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            Kullanici res = (from r in entities.Kullanicis where r.KullaniciAdi == KullaniciId select r).First();
            var res2 = from r in entities.Hakems where r.KullaniciAdi == KullaniciId select r;
            if(res2.Count()!=0){
                entities.DeleteObject(res2);
            }
            var res3 = from r in entities.Yazars where r.KullaniciAdi == KullaniciId select r;
            if (res3.Count()!=0){
                Yazar res13 = (from r in entities.Yazars where r.KullaniciAdi == KullaniciId select r).First();
               entities.DeleteObject(res13);
            }
           var res4 = from r in entities.Editors where r.KullaniciAdi == KullaniciId select r;
            if (res4.Count() != 0)
            {
                Editor res14 = (from r in entities.Editors where r.KullaniciAdi == KullaniciId select r).First();
                entities.DeleteObject(res14);
            }
            var res5 = from r in entities.Admins where r.KullaniciAdi == KullaniciId select r;
            if (res5.Count() != 0)
            {
                Admin res15 = (from r in entities.Admins where r.KullaniciAdi == KullaniciId select r).First();
                entities.DeleteObject(res15);
            }

            entities.DeleteObject(res);
            entities.SaveChanges();
        
        }

        public List<DBclass.KullaniciDBTO> KullaniciListele()
        {
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            var res = from r in entities.Kullanicis select r;
            List<DBclass.KullaniciDBTO> abc = res.ToList().ConvertAll(new Converter<Kullanici, DBclass.KullaniciDBTO>(Converter.Convert_Kullanici_TO_KullaniciDBTO));
            return abc;
        }

        public List<DBclass.HakemDBTO> HakemListele()
        {
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            var res = from r in entities.Hakems select r;
            List<DBclass.HakemDBTO> abc = res.ToList().ConvertAll(new Converter<Hakem, DBclass.HakemDBTO>(Converter.Convert_Hakem_TO_HakemDBTO));
            return abc;
        }

        public List<DBclass.YazarDBTO> YazarListele()
        {
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            var res = from r in entities.Yazars select r;
            List<DBclass.YazarDBTO> abc = res.ToList().ConvertAll(new Converter<Yazar, DBclass.YazarDBTO>(Converter.Convert_Yazar_TO_YazarDBTO));
            return abc;
        }

        public DBclass.KullaniciDBTO KullaniciBul(string kullaniciId) {
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            Kullanici res = (from r in entities.Kullanicis where r.KullaniciAdi == kullaniciId select r).First();
            return Converter.Convert_Kullanici_TO_KullaniciDBTO(res);
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////
        //Anket
        public void AnketEkle(DBclass.AnketDBTO obj)
        {
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            Anket insertingobject = Converter.Convert_AnketDBTO_TO_Anket(obj);
           /* insertingobject.AnketID = obj.AnketID;
            insertingobject.HakemID = obj.HakemID;
            insertingobject.MakaleID = obj.MakaleID;
            insertingobject.Sorular = obj.Sorular;
            insertingobject.Cevaplar = obj.Cevaplar;
            */
            entities.AddToAnkets(insertingobject);
            entities.SaveChanges();
        }

        public void AnketGuncelle(DBclass.AnketDBTO obj)
        {
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            Anket res = (from r in entities.Ankets where r.AnketID == obj.AnketID select r).First();
            res.AnketID = obj.AnketID;
            res.HakemID = obj.HakemID;
            res.MakaleID = obj.MakaleID;
            res.Sorular = obj.Sorular;
            res.Cevaplar = obj.Cevaplar;
            entities.SaveChanges();
        }

        public void AnketSil(string AnketId)
        {
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            Anket res2 = (from r in entities.Ankets where r.AnketID == AnketId select r).First();
            entities.DeleteObject(res2);
            entities.SaveChanges();
        }

        public List<DBclass.AnketDBTO> AnketListele()
        {
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            var res = from r in entities.Ankets select r;
            List<DBclass.AnketDBTO> abc = res.ToList().ConvertAll(new Converter<Anket, DBclass.AnketDBTO>(Converter.Convert_Anket_TO_AnketDBTO));
            return abc;
        }

        public DBclass.AnketDBTO AnketBul(string anketId)
        {
            EYAYINCILIK_DBEntities entities = new EYAYINCILIK_DBEntities();
            Anket res = (from r in entities.Ankets where r.AnketID == anketId select r).First();
            return Converter.Convert_Anket_TO_AnketDBTO(res);
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////


    }
}
